# Contributor: Guilherme Felipe da Silva <gfsilva.eng@gmail.com>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
pkgname=lua-pgmoon
_rockname="${pkgname#lua-}"
pkgver=1.16.0
pkgrel=0
pkgdesc="A pure Lua Postgres driver for use in OpenResty & more"
url="https://github.com/leafo/pgmoon"
license="MIT"
arch="all"
depends="lua-bitop lua-lpeg"
makedepends="luarocks"
options="!check" # Test suite is broken
source="$_rockname-$pkgver.tar.gz::https://github.com/leafo/$_rockname/archive/v$pkgver.tar.gz"
builddir="$srcdir/$_rockname-$pkgver"

_luaversions="5.1 5.2 5.3 5.4"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev luarocks$_v"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
done

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."

		luarocks-$lver \
			LUA_INCDIR="$(pkg-config --variable=includedir lua$lver)" \
			LUA_LIBDIR="$(pkg-config --variable=libdir lua$lver)" \
			make --tree="./build" \
			--deps-mode=none \
			"$_rockname-dev-1.rockspec"

		rm "./build/lib/luarocks/rocks-$lver/manifest"
	done
}

package() {
	mkdir -p "$pkgdir"
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"

	local path; for path in "lib/luarocks/rocks-$lver" "share/lua/$lver"; do
		mkdir -p "$subpkgdir/usr/${path%/*}"
		mv "$builddir/build/$path" "$subpkgdir/usr/$path/"
	done
}

sha512sums="
004b11227c00354d676e34a8059abed2c2a43fc1c7a814c9b7fd3e3b71baba8ed829270ed052dc837dcffc444c9586e020b68d603776410adec293a5086aa61a  pgmoon-1.16.0.tar.gz
"
